package com.enniswu.weitter.moment.post;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BasePhotoActivity;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.util.ImageUtil;
import com.enniswu.weitter.widget.adapter.AlbumAdapter;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PostActivity extends BasePhotoActivity<PostContract.Presenter> implements PostContract.View {

    @BindView(R.id.iv_portrait)
    ImageView ivPortrait;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_variety)
    TextView tvVariety;
    @BindView(R.id.et_context)
    EditText etContext;
    @BindView(R.id.rv_album)
    RecyclerView rvAlbum;
    private AlbumAdapter adapter;
    private int type;
    private User user;
    private List<String> albumPath;
    private List<File> album;
    private CropOptions cropOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        type = getIntent().getIntExtra("type", type);
        user = DBSupport.getInstance().getCurrentUser();
        Glide.with(this).load(user.getPortrait())
                .apply(new RequestOptions().circleCrop()).into(ivPortrait);
        tvName.setText(R.string.post_title);
        tvVariety.setText(user.getName());
        albumPath = new ArrayList<>(9);
        album = new ArrayList<>(9);
        cropOptions = new CropOptions.Builder().setWithOwnCrop(false).create();
        adapter = new AlbumAdapter(albumPath);
        adapter.setClickListener(adapter.new MultiOnItemClickListener() {
            @Override
            public void onDeleteClick(View v, int position) {
                super.onDeleteClick(v, position);
                album.remove(position);
            }
        });
        rvAlbum.setNestedScrollingEnabled(false);
        rvAlbum.setLayoutManager(new GridLayoutManager(this, 3));
        rvAlbum.setAdapter(adapter);
        new PostPresenter(type, this);
    }

    @Override
    public void next(int id) {
        if (id > 0) {
            Intent intent = new Intent();
            intent.putExtra("type", type);
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        List<TImage> images = result.getImages();
        if (images == null || images.isEmpty())
            return;
        for (TImage image : images) {
            String path = image.getOriginalPath();
            albumPath.add(path);
            album.add(new File(path));
        }
        notifyDataInserted(images.size());
    }

    @Override
    public void takeFail(TResult result, String msg) {
        showMessage(msg);
    }

    @OnClick({R.id.iv_album, R.id.iv_camera, R.id.iv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_album:
                if (album.size() < 9)
                    getTakePhoto().onPickFromGalleryWithCrop(
                            ImageUtil.getCropUri(this), cropOptions);
                    //getTakePhoto().onPickMultiple(9 - album.size());
                break;
            case R.id.iv_camera:
                if (album.size() < 9)
                    getTakePhoto().onPickFromCaptureWithCrop(
                            ImageUtil.getCropUri(this), cropOptions);
                break;
            case R.id.iv_send:
                presenter.post(user.getId(), etContext.getText().toString().trim(), album);
                break;
        }
    }

    private void notifyDataInserted(final int count) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemRangeInserted(albumPath.size() - count, count);
            }
        });
    }
}
